package com.secneo.appshare.activities;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;

import com.secneo.appshare.R;
import com.secneo.appshare.adapter.ActivityAdapter;
import com.secneo.appshare.adapter.ActivityAppDetailAdapter;
import com.secneo.appshare.api.RemoteApi.LatestActivity;
import com.secneo.appshare.common.ActivityUtil;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.threads.ActivityThread;

public class MainAppDetailActivity extends Activity
{
	public static boolean mRecyleFlag = false;
	private ProgressDialog mProgressDialog = null;
	private ActivityThread mAppDeatilThread = null;
	private ListView mListView = null;
	private ActivityAppDetailAdapter mAdapter = null;
	
	private Handler mHandler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case Constants.MSG_GET_RECOMS_NETWORK_ERROR:
					if (mProgressDialog != null)
					{
						mProgressDialog.dismiss();
						mProgressDialog = null;
					}
					View view = findViewById(R.id.view_network_error);
					view.setVisibility(View.VISIBLE);
					break;

				case Constants.MSG_GET_ACTIVITY_FINISH:
					showLogsListView(mAppDeatilThread.getActivitys());
					break;

				case Constants.MSG_GET_ACTIVITYICON_FINISH:
					mAdapter.notifyDataSetChanged();
					break;
			}
			super.handleMessage(msg);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_apps_detail);
		
		mListView = (ListView) findViewById(R.id.some_apps);
		mListView.setDivider(getResources().getDrawable(R.drawable.list_divider));
		mListView.addFooterView(showLayout());
		mListView.setOnScrollListener(mScrollListner);
		
		Constants.nowTime=getnewTime();
		startProgressDialog();
		mAppDeatilThread = new ActivityThread(MainAppDetailActivity.this, mHandler, "", mPagecount, Constants.nowTime);
		mAppDeatilThread.start();
	}

	@Override
	protected void onPause()
	{
		if(mRecyleFlag)
		{
			//回收资源
			mRecyleFlag = false;
			endChildrenThreads();
		}
		super.onPause();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		this.finish();
		return true;
	}

	@Override
	protected void onNewIntent(Intent intent)
	{
		Boolean needInit = intent.getBooleanExtra("FromGroup", false);
		if(needInit)
		{
			//重新获取资源
			startProgressDialog();
			endChildrenThreads();
			Constants.nowTime = getnewTime();
			mAllowGetLogAgain = false;
			searchLayout.setVisibility(View.VISIBLE);
			mAppDeatilThread = new ActivityThread(MainAppDetailActivity.this, mHandler, "", mPagecount, Constants.nowTime);
			mAppDeatilThread.start();
		}
		super.onNewIntent(intent);
	}
	
	private void startProgressDialog()
	{
		View view = findViewById(R.id.view_network_error);
		view.setVisibility(View.GONE);
		mListView.setVisibility(View.GONE);
		if (mProgressDialog != null)
			mProgressDialog.dismiss();
		mProgressDialog = new ProgressDialog(MainAppDetailActivity.this);
		mProgressDialog.setMessage("加载中，请稍候...");
		mProgressDialog.setCancelable(false);
		mProgressDialog.show();
	}
	
	private String getnewTime() 
	{
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(date);
	}
	
	private void endChildrenThreads()
	{
		if (mAppDeatilThread != null)
		{
			mAppDeatilThread.stopRun();
			mAppDeatilThread = null;
		}

		mListView.setAdapter(null);
		if (mAdapter != null)
			mAdapter.freeDrawable();
		
		mAllowGetLogAgain = true;
		mPageNum = 1;
		mItemSum = 0;
	}
	
	public void showLogsListView(List<LatestActivity> list) 
	{
		if (list == null)
		{
			if (mProgressDialog != null)
			{
				mProgressDialog.dismiss();
				mProgressDialog = null;
				searchLayout.setVisibility(View.GONE);
			}
			return;
		}
		mListView.setVisibility(View.VISIBLE);
		if(mPageNum == 1)
		{
			mAdapter = new ActivityAppDetailAdapter(this, list);
			if (mProgressDialog != null)
			{
				mProgressDialog.dismiss();
				mProgressDialog = null;
			}
			mListView.setAdapter(mAdapter);
		}
		else
		{
			mAdapter.appandAdapter(list);
			mAdapter.notifyDataSetChanged();
			mItemSum = mAdapter.getCount();
			int test12 = mItemSum % 12;
			if(test12 == 0)
				mListView.setSelection(mItemSum - 12);
			else
				mListView.setSelection(mItemSum - test12);
		}
	}
	
	// 以下为分页显示代码
	private static LinearLayout searchLayout = null;
	public LinearLayout showLayout()
	{
		searchLayout = new LinearLayout(this);
		searchLayout.setOrientation(LinearLayout.HORIZONTAL);

		ProgressBar progressBar = new ProgressBar(this);
		progressBar.setPadding(0, 0, 15, 0);
		progressBar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.progress_medium));
		searchLayout.addView(progressBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

		TextView textView = new TextView(this);
		textView.setText("加载中...");
		textView.setGravity(Gravity.CENTER_VERTICAL);
		searchLayout.addView(textView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
		searchLayout.setGravity(Gravity.CENTER);

		LinearLayout loadingLayout = new LinearLayout(this);
		loadingLayout.addView(searchLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		loadingLayout.setGravity(Gravity.CENTER);
		return loadingLayout;
	}
	
	private static boolean mAllowGetLogAgain = false;
	private static int mItemSum = 0;
	private int mPageNum = 1;
	private final int mPagecount = 12;
	public static void setAllowGetPageAgain()
	{
		int test12 = mItemSum % 12;
		if((test12 != 0) || (mItemSum >= 96))
		{
			mAllowGetLogAgain = false;
			searchLayout.setVisibility(View.GONE);
		}
		else
			mAllowGetLogAgain = true;
	}
	
	private OnScrollListener mScrollListner = new OnScrollListener()
	{
		private int lastItem = 0;
		
		@Override
		public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
		{
			lastItem = firstVisibleItem + visibleItemCount - 1;
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState)
		{
			if (mAdapter != null && lastItem == mAdapter.getCount() && scrollState == OnScrollListener.SCROLL_STATE_IDLE)
			{
				if (!mAllowGetLogAgain)
					return;
				mAllowGetLogAgain = false;
				mPageNum++;
				mAppDeatilThread = new ActivityThread(MainAppDetailActivity.this, mHandler, "", mPagecount, Constants.nowTime);
				mAppDeatilThread.start();
			}
		}
	};
}